package com.cola.action;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.cola.entity.User;
import com.cola.entity.courier.Courier;
import com.cola.entity.courier.SignGoods;
import com.cola.entity.order.Order;
import com.cola.enums.OrderType;
import com.cola.enums.UserType;
import com.cola.service.CourierService;
import com.cola.service.ShopService;
import com.cola.utils.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 配送员Action
 * @author Max
 * @since 2017/9/14
 */
public class CourierAction extends BaseAction {
    private String order_no;
    private String goods_final_json;
    private String scan_check_code;

    /**
     * 配送员首页
     * @author Max
     * @since 2017/9/13
     */
    public String courierIndex(){
        return SUCCESS;
    }

    /**
     * 配送员首页
     * @author Max
     * @since 2017/9/13
     */
    public void getCourierAllOrderList(){

        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();

        //获取配送员订单列表
        List<Order> list = CourierService.getInstance().getCourierAllOrderList(user.getUserId());
        resultMap.put("result","success");
        resultMap.put("list",list);
        Struts2Utils.renderJson(resultMap);
    }


    /**
     * 跳转到订单签收页面
     * @author Max
     * @since 2017/9/20
     */
    public String toSingOrderPage(){
        User user = getUserInfoFromRedis();
        Order order = ShopService.getInstance().getOrderInfoByOrderNo(order_no);
        if(order.getOrder_status().equals(OrderType.WAITING_FOR_DELIVERY.getValue())){
            setRequestAttribute("order",order);
            setWxMessage();
            //获取参与的促销信息
            setRequestAttribute("sale_activity", JSON.toJSONString(CourierService.getInstance().getJoinSaleActivityByOrderNo(order_no)));
            return SUCCESS;
        }else{
            return "index";
        }

    }

    /**
     * 签收
     * @author Max
     * @since 2017/9/20
     */
    public void doSignOrder(){
        User user = getUserInfoFromRedis();
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("result","error");
        boolean userSignStatus = false;
        if(user.getUserType()!=null){
            Order order = ShopService.getInstance().getOrderInfoByOrderNo(order_no);
            if(order.getOrder_status().equals(OrderType.WAITING_FOR_DELIVERY.getValue())){
                if(order.getCheck_code().equals(scan_check_code)){
                    if(user.getUserType().equals(UserType.COURIER.getValue())){
                        userSignStatus = user.getUserId().equals(order.getCourier_id());
                    }else if(user.getUserType().equals(UserType.MEP.getValue())){
                        userSignStatus = order.getCourier_id().equals(0) && order.getMep_no().equals(user.getColaNo());
                    }

                    if(userSignStatus){
                        List<SignGoods> list = JSONObject.parseObject(goods_final_json,new TypeReference<List<SignGoods>>(){});

                        if(CourierService.getInstance().doSignOrder(order,list)){
                            resultMap.put("result","success");
                        }else{
                            resultMap.put("msg","系统异常！");
                        }
                    }else{
                        resultMap.put("msg","请扫描自己的订单！");
                    }
                }else{
                    resultMap.put("msg","订单不正确！");
                }
            }else{
                resultMap.put("msg","订单已经签收！");
            }
        }else{
            resultMap.put("msg","签收异常");
        }
        Struts2Utils.renderJson(resultMap);
    }

    /**
     * 跳转到订单完成页面
     * @author Max
     * @since 2017/9/20
     */
    public String toSignFinished(){
        return SUCCESS;
    }

    /**
     * 跳转到订单完成页面
     * @author Max
     * @since 2017/9/20
     */
    public String toCourierFinishedOrder(){

        User user = getUserInfoFromRedis();
        if(user.getUserType()!=null){
            Order order = ShopService.getInstance().getOrderInfoByOrderNo(order_no);
            setRequestAttribute("order",order);
            return SUCCESS;
        }else{
            return ERROR_500;
        }
    }

    /**
     * 跳转到订单完成页面
     * @author Max
     * @since 2017/9/20
     */
    public String toCourierInfo(){
        User user = getUserInfoFromRedis();
        Courier courier = CourierService.getInstance().getCourierInfoById(user.getUserId());
        setRequestAttribute("user",courier);
        return SUCCESS;
    }



    /**
     * 跳转到各个页面时配置页面wx信息
     * @author Max
     * @since 2017/9/12
     */
    private void setWxMessage(){
        String jsapi_ticket= WeChatUtils.getJsApiTicket();
        String requestUrl = getRequestUrl(getRequest());
        long timestamp = System.currentTimeMillis();//时间戳
        String noncestr = NonceStrUtils.generateCommonStr(16);//随机字符串
        String SHA1data = "jsapi_ticket="+jsapi_ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+requestUrl;
        String signature = new SHA1().getDigestOfString(SHA1data.getBytes());
        //设置分享链接
        setRequestAttribute("shareUrl", PropertyUtils.getProperty("sendfriendurl"));
        setRequestAttribute("appid", PropertyUtils.getProperty("appid"));
        setRequestAttribute("signature", signature);
        setRequestAttribute("timestamp", timestamp);
        setRequestAttribute("noncestr", noncestr);
        setRequestAttribute("url", PropertyUtils.getProperty("url"));
    }


    public String getOrder_no() {
        return order_no;
    }

    public void setOrder_no(String order_no) {
        this.order_no = order_no;
    }

    public String getGoods_final_json() {
        return goods_final_json;
    }

    public void setGoods_final_json(String goods_final_json) {
        this.goods_final_json = goods_final_json;
    }

    public String getScan_check_code() {
        return scan_check_code;
    }

    public void setScan_check_code(String scan_check_code) {
        this.scan_check_code = scan_check_code;
    }
}
